package com.lmx.animationdemo;

import android.os.Bundle;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.AnimationUtils;
import android.view.animation.AnticipateInterpolator;
import android.view.animation.BounceInterpolator;
import android.view.animation.DecelerateInterpolator;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @author Leung
 * @date 2020/4/16 16:52
 */
public class MainActivity extends AppCompatActivity {

    @BindView(R.id.tvAnimation)
    TextView tvAnimation;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
    }

    @OnClick({R.id.btnTranslation, R.id.btnScale, R.id.btnRotate, R.id.btnAlpha, R.id.btnSet})
    public void onViewClicked(View view) {
        // 方法一：通过xml方式实现动画效果
        Animation animation = null;
        switch (view.getId()) {
            case R.id.btnTranslation:
                animation = AnimationUtils.loadAnimation(this, R.anim.translate_anim);
                break;
            case R.id.btnScale:
                animation = AnimationUtils.loadAnimation(this, R.anim.scale_anim);
                break;
            case R.id.btnRotate:
                animation = AnimationUtils.loadAnimation(this, R.anim.rotate_anim);
                break;
            case R.id.btnAlpha:
                animation = AnimationUtils.loadAnimation(this, R.anim.alpha_anim);
                break;
            case R.id.btnSet:
                animation = AnimationUtils.loadAnimation(this, R.anim.set_anim);
            default:
        }
        tvAnimation.startAnimation(animation);
        // 方法二：通过代码动态的实现动画效果
//        TranslateAnimation translateAnimation = new TranslateAnimation(0, 0, 0, 800);
//        translateAnimation.setDuration(2000);
//        tvAnimation.startAnimation(translateAnimation);
//        ScaleAnimation scaleAnimation = new ScaleAnimation(1.0f, 0.0f, 1.0f, 0.0f, tvAnimation.getWidth() / 2, tvAnimation.getHeight() / 2);
//        scaleAnimation.setDuration(2000);
//        tvAnimation.startAnimation(scaleAnimation);
//        RotateAnimation rotateAnimation = new RotateAnimation(0, 360, tvAnimation.getWidth() / 2, tvAnimation.getHeight() / 2);
//        rotateAnimation.setDuration(2000);
//        tvAnimation.startAnimation(rotateAnimation);
//        AlphaAnimation alphaAnimation = new AlphaAnimation(1.0f, 0.0f);
//        alphaAnimation.setDuration(2000);
//        tvAnimation.setAnimation(alphaAnimation);
//        AnimationSet animationSet = new AnimationSet(true);
//        animationSet.setDuration(4000);
//        animationSet.setInterpolator(new DecelerateInterpolator());
//        animationSet.addAnimation(translateAnimation);
//        animationSet.addAnimation(scaleAnimation);
//        animationSet.addAnimation(rotateAnimation);
//        animationSet.addAnimation(alphaAnimation);
//        tvAnimation.startAnimation(animationSet);
    }
}
